/**
 * Created by 86185 in 2020/8/15 on 17:39
 */
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const {CleanWebpackPlugin} = require("clean-webpack-plugin");

const pro = process.env.production === "true";

const frontendConfig = {
    srcPath: path.resolve(__dirname, "./src"),
    distPath: path.resolve(__dirname, "./dist")
};

module.exports = {
    mode: pro ? "production" : "development",
    entry: {
        app: `${frontendConfig.srcPath}/main.js`
    },
    output: {
        path: frontendConfig.distPath,
        filename: "[name].bundle.js",
        publicPath: !pro ? "http://127.0.0.1:9991/frontend/public/dist/" : ""
    },
    resolve: {
        extensions: [".js", "jsx"],
        alias: {
            "@": frontendConfig.srcPath
        }
    },
    externals: {
        jquery: "jquery",
        echarts: "echarts",
        lodash: "_",
        maptalks: "maptalks"
    },
    target: "web",
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            filename: "index.html",
            template: `!!html-loader!${path.resolve(__dirname, "./index.html")}`,
            inject: "body",
            favicon: `${frontendConfig.srcPath}/assets/favicon.ico`
        })
    ],
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ["@babel/preset-env"]
                    }
                }
            },
            {
                test: /\.styl(us)?$/,
                use: [
                    "style-loader",
                    "css-loader",
                    "postcss-loader",
                    "stylus-loader"
                ]
            },
            {
                test: /\.css$/,
                use: [
                    "style-loader",
                    "css-loader",
                    "postcss-loader",
                ]
            },
            {
                test: /\.(png|svg|jpe?g|gif)$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            limit: 4096,
                            fallback: {
                                loader: "file-loader",
                                options: {
                                    name: "img/[name].[hash:8].[ext]"
                                }
                            }
                        }
                    }
                ]
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            limit: 4096,
                            fallback: {
                                loader: "file-loader",
                                options: {
                                    name: "fonts/[name].[hash:8].[ext]"
                                }
                            }
                        }
                    }
                ]

            }
        ]
    },
    devtool: pro ? "none" : "inline-source-map",
    devServer: {
        proxy: {
            "/ly-web": {
                target: "http://127.0.0.1:8666",
                pathRewrite: {},
                changeOrigin: true,
                secure: true
            }
        }
    },
};